package com.etrans.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>
 * author: jw.chen
 * </p>
 * <p>
 * Title: StringUtil.java
 * </p>
 * <p>
 * Create on 2015-1-30下午5:09:03
 * </p>
 * <p>
 * Copyright (c) 2015-1-30 by e_trans.
 * </p>
 */

public class StringUtil {
	protected static Log log = LogFactory.getLog(StringUtil.class);

	public static String integerArrayToString(Object[] array) {
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < array.length; i++) {
			if (!"".equals(str.toString()))
				str.append(",");
			str.append(array[i].toString());
		}
		return str.toString();
	}

	public static String interceptStr(String str, int length) {
		return str.length() > length ? str.substring(0, length) + "..." : str;
	}

	// Format your date as the specify formation.
	// @@strDate: your date
	// @@strFormat: the specify formation
	// Return blank string '' when the first parameter is null or any error
	// occured
	public static String[] split(String strSource, String strDiv) {
		int arynum = 0, intIdx = 0, intIdex = 0;
		int div_length = strDiv.length();
		if (strSource.compareTo("") != 0) {
			if (strSource.indexOf(strDiv) != -1) {
				intIdx = strSource.indexOf(strDiv);
				for (int intCount = 1;; intCount++) {
					if (strSource.indexOf(strDiv, intIdx + div_length) != -1) {
						intIdx = strSource.indexOf(strDiv, intIdx + div_length);
						arynum = intCount;
					} else {
						arynum += 2;
						break;
					}
				}
			} else {
				arynum = 1;
			}
		} else {
			arynum = 0;

		}
		intIdx = 0;
		intIdex = 0;
		String[] returnStr = new String[arynum];

		if (strSource.compareTo("") != 0) {
			if (strSource.indexOf(strDiv) != -1) {
				intIdx = (int) strSource.indexOf(strDiv);
				returnStr[0] = (String) strSource.substring(0, intIdx);
				for (int intCount = 1;; intCount++) {
					if (strSource.indexOf(strDiv, intIdx + div_length) != -1) {
						intIdex = (int) strSource.indexOf(strDiv, intIdx + div_length);
						returnStr[intCount] = (String) strSource.substring(intIdx + div_length, intIdex);
						intIdx = (int) strSource.indexOf(strDiv, intIdx + div_length);
					} else {
						returnStr[intCount] = (String) strSource.substring(intIdx + div_length, strSource.length());
						break;
					}
				}
			} else {
				returnStr[0] = (String) strSource.substring(0, strSource.length());
				return returnStr;
			}
		} else {
			return returnStr;
		}
		return returnStr;
	}

	// Replace the null string with blank "".
	public static String dealNull(String str) {
		String returnstr = "";
		if (str != null) {
			returnstr = str;
		}
		return returnstr;
	}

	// Replace the specified string "strSub" with string "strRpl".
	// eg:replace(txt,"\n","<BR>");
	public static String replace(String str, String strSub, String strRpl) {
		String[] tmp = split(str, strSub);
		String returnstr = "";
		if (tmp.length != 0) {
			returnstr = tmp[0];
			for (int i = 0; i < tmp.length - 1; i++) {
				returnstr = dealNull(returnstr) + strRpl + tmp[i + 1];
			}
		}
		return dealNull(returnstr);
	}

	// Format string include HTML tags for storage.
	public static String htmlEncode(String txt) {
		if (txt != null) {
			txt = replace(txt, "&", "&amp;");
			txt = replace(txt, "&amp;amp;", "&amp;");
			txt = replace(txt, "&amp;quot;", "&quot;");
			txt = replace(txt, "\"", "&quot;");
			txt = replace(txt, "&amp;lt;", "&lt;");
			txt = replace(txt, "<", "&lt;");
			txt = replace(txt, "&amp;gt;", "&gt;");
			txt = replace(txt, ">", "&gt;");
			txt = replace(txt, "&amp;nbsp;", "&nbsp;");
			// txt = replace(txt," ","&nbsp;");
		}
		return txt;
	}

	// Restore string include HTML tags from storage.
	public static String unHtmlEncode(String txt) {
		if (txt != null) {
			txt = replace(txt, "&amp;", "&");
			txt = replace(txt, "&quot;", "\"");
			txt = replace(txt, "&lt;", "<");
			txt = replace(txt, "&gt;", ">");
			txt = replace(txt, "&nbsp;", " ");
		}
		return txt;
	}

	// Encode the string by GBK
	public static String toGBK(String str) {
		if (str != null) {
			byte[] tmpbyte = null;
			try {
				tmpbyte = str.getBytes("ISO8859_1");
			} catch (Exception e) {
				log.error("Error: Method: StringUtil.toGBK :" + e.getMessage());
			}
			try {
				if (tmpbyte != null) {
					str = new String(tmpbyte, "GBK");
				}
			} catch (Exception e) {
				log.error("Error: Method: StringUtil.toGBK :" + e.getMessage());
			}
		}
		return str;
	}

	// Encode the string by ISO8859_1
	public static String toISO(String str) {
		if (str != null) {
			byte[] tmpbyte = null;
			try {
				tmpbyte = str.getBytes("GBK");
			} catch (Exception e) {
				log.error("Error: Method: StringUtil.toISO :" + e.getMessage());
			}
			try {
				if (tmpbyte != null) {
					str = new String(tmpbyte, "ISO8859_1");
				}
			} catch (Exception e) {
				log.error("Error: Method: StringUtil.toISO :" + e.getMessage());
			}
		}
		return str;
	}

	// Encode the string by UTF-8
	public static String toUTF8(String str) {
		if (str != null) {
			byte[] tmpbyte = null;
			try {
				tmpbyte = str.getBytes("ISO-8859-1");
			} catch (Exception e) {
				log.error("Error: Method: StringUtil.toUTF8 :" + e.getMessage());
			}
			try {
				if (tmpbyte != null) {
					str = new String(tmpbyte, "UTF-8");
				}
			} catch (Exception e) {
				log.error("Error: Method: StringUtil.toUTF8 :" + e.getMessage());
			}
		}
		return str;
	}

	// Encode the string by UTF-8
	public static String toGBKUTF8(String str) {
		if (str != null) {
			byte[] tmpbyte = null;
			try {
				tmpbyte = str.getBytes("GBK");
			} catch (Exception e) {
				log.error("Error: Method: StringUtil.toUTF8 :" + e.getMessage());
			}
			try {
				if (tmpbyte != null) {
					str = new String(tmpbyte, "UTF-8");
				}
			} catch (Exception e) {
				log.error("Error: Method: StringUtil.toUTF8 :" + e.getMessage());
			}
		}
		return str;
	}

	public static String toUTF8GBK(String str) {
		if (str != null) {
			byte[] tmpbyte = null;
			try {
				tmpbyte = str.getBytes("UTF-8");
			} catch (Exception e) {
				log.error("Error: Method: StringUtil.toUTF8 :" + e.getMessage());
			}
			try {
				if (tmpbyte != null) {
					str = new String(tmpbyte, "GBK");
				}
			} catch (Exception e) {
				log.error("Error: Method: StringUtil.toUTF8 :" + e.getMessage());
			}
		}
		return str;
	}

	/**
	 * 在hibernate下使用Restrictions.like方法时，当用于匹配的字符串包含'_','%','[',']'时无法正确匹配，
	 * 需要使用Restrictions.sql来替换like方法。此方法便用于替换要匹配的字符串中的字符，使用escapeStr转义
	 * 
	 * @param likeStr
	 *            要替换的匹配字符串
	 * @param escapeStr
	 *            要作为转义字符的字符,建议使用符号'/'等
	 * @return
	 * @author Keven
	 */
	public static String escapeSQLLike(String likeStr, String escapeStr) {
		String str = replace(likeStr, escapeStr, escapeStr + escapeStr);
		str = replace(str, "_", escapeStr + "_");
		str = replace(str, "%", escapeStr + "%");
		str = replace(str, "[", escapeStr + "[");
		str = replace(str, "]", escapeStr + "]");
		str = replace(str, "'", "''");
		return str;
	}

	/**
	 * 正则表达式 验证邮箱格式是否正确
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(email);
		return m.find();
	}

	public static boolean isEmpty(String str) {
		if (str == null || str.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isNotEmpty(String str) {
		if (str != null && !str.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}

	// Set view language.
	public void setLocale(String locale) {
		// to be completed
	}

	// Set page content type.
	public void setContentType(String contentType) {
		// to be completed
	}
		/**
		 * 根据逗号来截取数据
		 * 
		 * @param servType
		 * @return
		 */
		public static List<String> spiltString(String servType) {
			List<String> list = new ArrayList<String>();
			String[] arr = servType.split(",");
			for (int i = 0; i < arr.length; i++) {
				list.add(arr[i]);
			}
			return list;
	}
}
